<?php
/**
 * Author  : zxz
 * Datetime: 2019/3/17 下午7:16
 */

namespace Xy\Rbac\Repositories;



use Xy\Rbac\Models\RbacRoleModel;

class RbacRoleRepository extends BaseRepository {
    public $eloquentClassName = RbacRoleModel::class;
    
    public function getPage() {
        return $this->queryByConditionAndPage();
    }
    
    /**
     * @param array $dataInfo
     * @param array $fillAble
     *
     * @return RbacRoleModel
     */
    public function storeRbacRole(array $dataInfo, array $fillAble) {
        /** @var RbacRoleModel $newItem */
        $newItem = $this->_eloquent->newInstance();
        $newItem->fillable($fillAble)->fill($dataInfo);
        $newItem->save();
        
        return $newItem;
    }
    
    /**
     * @param $id
     * @param $dataInfo
     * @param $fillAble
     *
     * @return RbacRoleModel|null
     */
    public function updateRbacRoleById($id, $dataInfo, $fillAble) {
        /** @var RbacRoleModel $updateItem */
        $updateItem = $this->_eloquent->newQuery()->find($id);
        if ($updateItem) {
            $updateItem->fillable($fillAble)->fill($dataInfo)->save();
        }
        
        return $updateItem;
    }
    
    
    /**
     * @param $id
     *
     * @return RbacRoleModel
     * @throws \Exception
     */
    public function destroyPowerRbacRole($id) {
        /** @var RbacRoleModel $deleteItem */
        $deleteItem = $this->_eloquent->newQuery()->find($id);
        $deleteItem && $deleteItem->delete();
        
        return $deleteItem;
    }
    
    /**
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getAllRbacRole($isStatusYes = true)
    {
        $query = $this->_eloquent->newQuery();
        $isStatusYes && $query->where('status', RbacRoleModel::STATUS_YES);
        
        return $query->get();
    }
}